package com.ruixi.spring.jdbcTemplete;

import com.ruixi.spring.jdbcTemplete.entity.Book;
import com.ruixi.spring.jdbcTemplete.service.BookService;
import com.ruixi.spring.jdbcTemplete.service.BookServiceImpl;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.List;

public class TestJdbcTemplate {

    BookService bookService;

    @Before
    public void getTemplate(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("conf/jdbc.xml");
        BookService bookService = applicationContext.getBean("bookService", BookServiceImpl.class);
        this.bookService = bookService;
    }

    //添加
    @Test
    public void add(){
        Book book = new Book();
        book.setId("1");
        book.setName("Java从删库到跑路");
        book.setStatus("正常");
        this.bookService.add(book);
    }

    //修改
    @Test
    public void update(){
        Book book = new Book();
        book.setId("1");
        book.setName("Elasticsearch从入门到精通");
        book.setStatus("正常");
        this.bookService.update(book);
    }

    //删除
    @Test
    public void delete(){
        this.bookService.delete("1");
    }

    //查询 返回某个值
    @Test
    public void findCount(){
        Integer count = this.bookService.findCount();
        System.out.println("总记录数:" + count);
    }

    //查询 返回对象
    @Test
    public void findByID(){
        Book book = this.bookService.findByID("1");
        System.out.println(book);
    }

    //查询 返回集合
    @Test
    public void findAll(){
        List<Book> list = this.bookService.findAll();
        System.out.println(list);
    }

    //批量添加
    @Test
    public void saveBatch(){
        List<Object[]> list = new ArrayList<>();
        Object[] o1 = {"1","Java从入门到入土","正常"};
        Object[] o2 = {"2","MySql从删库到跑路","正常"};
        Object[] o3 = {"3","Elasticsearch从入门到入土","正常"};
        list.add(o1);
        list.add(o2);
        list.add(o3);
        this.bookService.saveBatch(list);
    }

    //批量修改
    @Test
    public void updateBatch(){
        List<Object[]> list = new ArrayList<>();
        Object[] o1 = {"Java从入门到入土","正常","2"};
        Object[] o2 = {"MySql从删库到跑路","正常","1"};
        Object[] o3 = {"Elasticsearch从入门到入土","正常","3"};
        list.add(o1);
        list.add(o2);
        list.add(o3);
        this.bookService.updateBatch(list);
    }

    //批量删除
    @Test
    public void deleteBatch(){
        List<Object[]> list = new ArrayList<>();
        Object[] o1 = {"1"};
        Object[] o2 = {"2"};
        Object[] o3 = {"3"};
        list.add(o1);
        list.add(o2);
        list.add(o3);
        this.bookService.deleteBatch(list);
    }
}
